<template>
  <n-config-provider :theme-overrides="themeOverrides" :locale="zhCN">
    <router-view></router-view>
  </n-config-provider>
</template>

<script setup lang="ts">
import CacheUtil from '@renderer/util/CacheUtil'
import { Constants } from '@renderer/util/Constants'
import { ref, onMounted } from 'vue'
import { NConfigProvider } from 'naive-ui'
import { darkTheme, lightTheme } from 'naive-ui'
import EventBus from '@renderer/util/event/EventBus'
import { Events } from '@renderer/util/event/Events'
import { zhCN } from 'naive-ui'
import { useRouter } from 'vue-router'

const themeOverrides = ref(darkTheme)

onMounted(() => {
  if (CacheUtil.get<string>(Constants.THEME)) {
    let theme = CacheUtil.get<string>(Constants.THEME)
    switchTheme(theme)
  }
  EventBus.on(Events.SWITCH_THEME, switchTheme)
})

function switchTheme(text) {
  if (text == 'light') {
    themeOverrides.value = lightTheme
  } else if (text == 'dark') {
    themeOverrides.value = darkTheme
  }
}

const { ipcRenderer } = window.electron

const router = useRouter()

// 处理菜单事件
onMounted(() => {
  ipcRenderer.on('open-settings', () => {
    router.push('/settings')
  })
  
  ipcRenderer.on('open-main', () => {
    router.push('/')
  })

  ipcRenderer.on('open-about', () => {
    router.push('/about')
  })
})
</script>
